package controller;

import pojoCla.News;
import pojoCla.NewsChnl;
import pojoCla.SysUser;
import util.ChnlUtil;
import util.NewsUtil;
import util.UserUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet(name = "DoNews", value = "/doNews")
public class DoNews extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String method = request.getParameter("method");

        SysUser user = (SysUser) request.getSession().getAttribute("user");
        NewsUtil nu = new NewsUtil();
        Method dm = null;
        try {
            dm = this.getClass().getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class, NewsUtil.class,SysUser.class);
            dm.invoke(this,request,response,nu,user);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void fistLoad(HttpServletRequest request,HttpServletResponse response,NewsUtil nu,SysUser user){
        ChnlUtil cu = new ChnlUtil();
        HttpSession session = request.getSession();
        try {
            if("1".equals(user.getSysCode())){
                List<NewsChnl> sumChnlByUser = cu.getSumChnlByUser(user);
                List<News> newsPageListByUser = nu.getNewsPageListByUser(sumChnlByUser, 1, 5);
                Integer newsSumPageByUser = nu.getNewsSumPageByUser(sumChnlByUser, 5);
                session.setAttribute("newsChnlList",sumChnlByUser);
                session.setAttribute("newsPageList",newsPageListByUser);
                session.setAttribute("newsPageIndex",1);
                session.setAttribute("newsSumPage",newsSumPageByUser);
                response.sendRedirect("/page/NewsManagement.jsp");
                return;
            }
            List<NewsChnl> sumChnl = cu.getSumChnl();
            List<News> allNews = nu.getNewsPageList(1,5);
            Integer newsSumPage = nu.getNewsSumPage( 5);
            session.setAttribute("newsChnlList",sumChnl);
            session.setAttribute("newsPageList",allNews);
            session.setAttribute("newsPageIndex",1);
            session.setAttribute("newsSumPage",newsSumPage);
            response.sendRedirect("/page/NewsManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private void addNews(HttpServletRequest request,HttpServletResponse response,NewsUtil nu,SysUser user){
        HttpSession session = request.getSession();
        String addNewsCenter = request.getParameter("addNewsTitle");
        String addNewsKeyword = request.getParameter("addNewsKeyword");
        String addNewsChnl = request.getParameter("addNewsChnlName");
        int addNewsTop = request.getParameter("addNewsTop").equals("否") ? 0:1;
        String addNewsText = request.getParameter("addNewsText");
        try {
            nu.addNews(addNewsCenter,addNewsKeyword,addNewsChnl,addNewsTop,addNewsText,user.getAcct());
            this.updatePage(request,response,nu,user);
            session.setAttribute("addNewsMSG","发布成功");
            response.sendRedirect("/page/NewsManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private void checkTitle(HttpServletRequest request,HttpServletResponse response,NewsUtil nu,SysUser user){

            String newsTitle = request.getParameter("newsTitle");
            String newsChnlName = request.getParameter("newsChnlName");
        try {
            boolean newsByChnl = nu.findNewsByChnl(newsTitle, newsChnlName);
            String msg = newsByChnl ? "0":"1";
            PrintWriter writer = response.getWriter();
            writer.print(msg);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private void doUpdateNewsPage(HttpServletRequest request,HttpServletResponse response,NewsUtil nu,SysUser user){
        HttpSession session = request.getSession();
        try {
            this.updatePage(request,response,nu,user);
            response.sendRedirect("/page/NewsManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void selNewsByKeyword(HttpServletRequest request,HttpServletResponse response,NewsUtil nu,SysUser user){
        HttpSession session = request.getSession();
        try {
            String selNewsChnl = request.getParameter("selNewsChnl");
            String selNewsKeyword = request.getParameter("selNewsKeyword");
            session.setAttribute("newsPageIndex",1);
            if(selNewsChnl.equals("=请选择=") && selNewsKeyword.isEmpty()){
                session.removeAttribute("selNewsChnlName");
                session.removeAttribute("selNewsKeyword");
            }else{
                session.setAttribute("selNewsChnlName",selNewsChnl);
                session.setAttribute("selNewsKeyword",selNewsKeyword);
            }
            this.updatePage(request,response,nu,user);
            response.sendRedirect("/page/NewsManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void updatePage(HttpServletRequest request,HttpServletResponse response,NewsUtil nu,SysUser user){
        try {
            HttpSession session = request.getSession();
            String selNewsChnlName = (String) session.getAttribute("selNewsChnlName");
            String selNewsKeyword = (String) session.getAttribute("selNewsKeyword");
            String newsPageIndex = request.getParameter("newsPageIndex");

            Integer newsSumPage = null;
            Integer pageIndex = null;
            List<News> newsPageList = null;
            if("1".equals(user.getSysCode())){
                List<NewsChnl> chnlList = (List<NewsChnl>) session.getAttribute("newsChnlList");
                newsSumPage = selNewsChnlName == null && selNewsKeyword == null ?  nu.getNewsSumPageByUser(chnlList,5):nu.getSelSumPage(chnlList,selNewsChnlName,selNewsKeyword,5);
                pageIndex = newsPageIndex == null ? (Integer) session.getAttribute("newsPageIndex") : Integer.valueOf(newsPageIndex);
                pageIndex = pageIndex > 0 ?  pageIndex <=newsSumPage? pageIndex:newsSumPage   : 1;
                newsPageList = selNewsChnlName == null && selNewsKeyword == null ? nu.getNewsPageListByUser(chnlList,pageIndex,5):nu.selNewsByKeyword(chnlList,selNewsChnlName,selNewsKeyword,pageIndex,5);
            }else{
                newsSumPage = selNewsChnlName == null && selNewsKeyword == null ?  nu.getNewsSumPage(5):nu.getSelSumPage(selNewsChnlName,selNewsKeyword,5);
                pageIndex = newsPageIndex == null ? (Integer) session.getAttribute("newsPageIndex") : Integer.valueOf(newsPageIndex);
                pageIndex = pageIndex > 0 ?  pageIndex <=newsSumPage? pageIndex:newsSumPage   : 1;
                newsPageList = selNewsChnlName == null && selNewsKeyword == null ? nu.getNewsPageList( pageIndex,5):nu.selNewsByKeyword(selNewsChnlName,selNewsKeyword,pageIndex,5);
            }

            session.setAttribute("newsPageIndex",pageIndex);
            session.setAttribute("newsPageList",newsPageList);
            session.setAttribute("newsSumPage",newsSumPage);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
    private void setNewsTop(HttpServletRequest request,HttpServletResponse response,NewsUtil nu,SysUser user){
        String setNewsTop = request.getParameter("setNewsTop");
        String setNewsID = request.getParameter("setNewsID");
        try {
            nu.setNewsTop(setNewsTop,setNewsID);
            this.updatePage(request,response,nu,user);
            response.sendRedirect("/page/NewsManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void delNews(HttpServletRequest request,HttpServletResponse response,NewsUtil nu,SysUser user){
        String delNewsID = request.getParameter("delNewsID");
        try {
            nu.delNews(delNewsID);
            this.updatePage(request,response,nu,user);
            request.getSession().setAttribute("delNewsMSG","删除成功");
            response.sendRedirect("/page/NewsManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void updateNews(HttpServletRequest request,HttpServletResponse response,NewsUtil nu,SysUser user){
        String editNewsTitle = request.getParameter("editNewsTitle");
        String editNewsKeyword = request.getParameter("editNewsKeyword");
        String editNewsChnlName = request.getParameter("editNewsChnlName");
        int editNewsTop = request.getParameter("editNewsTop").equals("否") ? 0:1;
        String editNewsText = request.getParameter("editNewsText");
        String editNewsID = request.getParameter("editNewsID");
        try {
            nu.updateNews(editNewsTitle,editNewsKeyword,editNewsChnlName,editNewsTop,editNewsText,editNewsID);
            this.updatePage(request,response,nu,user);
            request.getSession().setAttribute("editNewsMSG","编辑成功");
            response.sendRedirect("/page/NewsManagement.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
